package com.lyl.cloud.controller;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import com.lyl.cloud.common.base.Result;
import com.lyl.cloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class PaymentController {
    private final PaymentService paymentService;

    @GetMapping("/test2")
    public Result test2(){
        System.out.println("feign客户端调用 test2接口 " + DateUtil.now());
        return paymentService.test2();
    }

    @GetMapping("/test")
    public String test(){
        System.out.println("feign客户端调用 test接口 " + DateUtil.now());
        return paymentService.test();
    }

    @GetMapping("/test/{id:\\d+}")
    @HystrixCommand(fallbackMethod = "hystrixTest3")
    public Result test3(@PathVariable("id") String id){
        System.out.println("feign客户端调用 test3接口 " + DateUtil.now() + " 参数为：" + id);
        return paymentService.test3(id);
    }

    public Result hystrixTest3(@PathVariable("id") String id){
        System.out.println("熔断：feign客户端调用 test3接口 " + DateUtil.now() + " 参数为：" + id);
        return Result.fail("启动断路器", ListUtil.empty());
    }

}
